define(function (require, exports, module) {
    require("apps/pbom/page.css");
    var dataImport = require("apps/data-import");
    require("apps/grid");

    exports.init = function (ctx, uuid) {
        var panel = dataImport.createImportPanel({
            keyCode: "PBOM_IMPORT"
        });

        function whenUpload(href) {
            panel.open();
        }

        function setInfo(pbom, id, children) {
            if (pbom.id == id) {
                pbom.children = children;
                return true;
            } else {
                if (pbom.children && pbom.children.length) {
                    var ex = false;
                    for (var i = 0, l = pbom.children.length; i < l; i++) {
                        ex = setInfo(pbom.children[i], id, children);
                        if (ex) {
                            return ex;
                        }
                    }
                }
                return false;
            }
        }

        var pageListComponent = new $.PageTreeListComponent(uuid, {
            view: {
                grid: "grid",
                myToolbar: "news_tb",
                colbar: "user_list_btn"
            },
            configs: {
                gridTitle: "工艺设计结构列表",
                gridUrl: ctx + "/app/api/pbom/list?root=true",
                idField: "id",
                treeField: "pName",
                gridColumns: [
                    {
                        field: 'id',
                        checkbox: true
                    },
                    {
                        field: 'temp1',
                        hidden: true
                    },
                    {
                        field: 'pName',
                        title: '名称',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: 'code',
                        title: '编码',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: 'number',
                        title: '数量（件）',
                        width: 50,
                        align: 'left'
                    },
                    {
                        field: 'bomType',
                        title: '制造分类',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: '_queryNumber',
                        title: '匹配的工件',
                        width: 100,
                        align: 'left',
                        formatter: function (value, row, index) {
                            return "总数：" + row.suitAll + "，未装配：" + row.nonBuild;
                        }
                    },
                    {
                        field: 'remark',
                        title: '备注',
                        hidden: true,
                        width: 50,
                        align: 'left'
                    },
                    {
                        field: 'status',
                        title: '状态',
                        width: 50,
                        align: 'left',
                        formatter: function (value, row, index) {
                            if (value == "PUBLISH") {
                                return "已发布";
                            } else if (value == "UN_PUBLISH") {
                                return "未发布";
                            } else {
                                return "已废弃";
                            }
                        }
                    }
                ],
                onExpand: function (row) {
                    if (row.children == undefined) {
                        pageListComponent.grid.treegrid("loadChildren", {
                            url: ctx + "/app/api/pbom/children",
                            row: row
                        });
                    }
                }
            },
            events: {
                renderColBarBefore: function (key, row) {
                    if (key == "publish") {
                        return row.status == "UN_PUBLISH"
                    } else if (key == "edit") {
                        return row.status == "UN_PUBLISH";
                    } else if (key == "build") {
                        return row.canBuild;
                    } else if (key == 'pBomDetail') {
                        return row.status == "PUBLISH" && row.suitAll > 0;
                    } else if (key == 'export') {
                        return row.status == "PUBLISH";
                    }
                    return true;
                },
                onClickColBtn: function (context, btn, row) {
                    switch (btn.key) {
                        case "view":
                            whenView(context, row);
                            break;
                        case "publish":
                            whenPublish(context, row);
                            break;
                        case "pBomDetail":
                            whenViewDetail(context, btn.href, row);
                            break;
                        case "del":
                            whenDel(context, row);
                            break;
                        case "export":
                            whenExport(context, row);
                            break;
                        default :
                            break;
                    }
                },
                onClickToolbarBtn: function (context, btn) {
                    switch (btn.key) {
                        case "search_all":
                            context.searchAll();
                            break;
                        case "search":
                            context.search(btn.href, "设计结构搜索", "pbom_search");
                            break;
                        case "upload":
                            whenUpload(btn.href);
                            break;
                        default :
                            break;
                    }
                }
            }
        });
        var globalWindow = new $.PageWindow();

        function whenViewDetail(context, href, row) {
            var id = row.refId ? row.refId : row.id;
            globalWindow.create({
                width: 700,
                href: href + "?id=" + id,
                title: "匹配的装配结构"
            });
            globalWindow.open();
        }

        function whenDel(context, row) {
            $.messager.confirm("确认？", "删除的PBOM结构不能再恢复，但不影响其子部件的使用，是否继续操作？", function (arg) {
                if (arg) {
                    $("body").wait("start", "数据删除中，请稍后！");
                    var queryParams = {id: row.id};
                    if (row.refId) {
                        queryParams.id = row.refId;
                    }
                    $.post(ctx + "/app/api/pbom/delPBom", queryParams, function (data) {
                        $("body").wait("end");
                        if ($.isPlainObject(data) && data.error) {
                            $.messager.alert('信息',
                                data.msg, 'info');
                        } else {
                            $.messager.alert('信息',
                                "数据删除成功！", 'info');
                            context.grid.treegrid("reload");
                        }
                    }, "JSON");
                }
            });
        }

        function whenPublish(context, row) {
            $.messager.confirm("确认？", "设计结构一旦发布，则该结构将不能再进行修改，且可被用于工艺结构的构建，是否继续？", function (arg) {
                if (arg) {
                    $("body").wait("start", "结构数据发布，请稍后！");
                    var queryParams = {id: row.id};
                    if (row.refId) {
                        queryParams.id = row.refId;
                    }
                    $.post(ctx + "/app/api/pbom/publish", queryParams, function (data) {
                        $("body").wait("end");
                        if ($.isPlainObject(data) && data.error) {
                            $.messager.alert('信息',
                                data.msg, 'info');
                        } else {
                            $.messager.alert('信息',
                                "保存成功！", 'info');
                            context.grid.treegrid("reload");
                        }
                    })
                }
            })
        }


        function whenExport(context, row) {
            $.messager.choose("请选择导出的设计结构是否需要编码",
                {
                    buttons: ["需要编码", "不需要"],
                    handler: function (i) {
                        var fileComp = require("apps/file");
                        if (i == 0) {
                            fileComp.download(WE.contextPath + "/app/api/pbom/export?hasCode=true&id=" + row.id);
                        } else {
                            fileComp.download(WE.contextPath + "/app/api/pbom/export?hasCode=false&id=" + row.id);
                        }
                    }});
        }

        function whenView(context, row) {
            context.detail("工艺设计结构详情查看", row);
        }
    }
});